package org.wisigoth.chat.client.mockup;

/**
 * MockUp implementation of the Session interface
 */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import javax.swing.event.EventListenerList;

import org.wisigoth.chat.client.ChatSession;
import org.wisigoth.chat.client.Conference;
import org.wisigoth.chat.client.ConnectionEvent;
import org.wisigoth.chat.client.ConnectionStateListener;
import org.wisigoth.chat.client.Contact;
import org.wisigoth.chat.client.DefaultPresence;
import org.wisigoth.chat.client.FriendList;
import org.wisigoth.chat.client.Message;
import org.wisigoth.chat.client.MessageConsumer;
import org.wisigoth.chat.client.Presence;
import org.wisigoth.chat.client.PresenceEvent;
import org.wisigoth.chat.client.PresenceListener;
import org.wisigoth.chat.client.Session;
import org.wisigoth.chat.client.Wisigoth;
import org.wisigoth.chat.client.WisigothUser;
import org.wisigoth.chat.client.exceptions.AuthenticationException;
import org.wisigoth.chat.client.exceptions.ConnectionException;
import org.wisigoth.chat.client.exceptions.FriendListException;
import org.wisigoth.chat.client.exceptions.MessageException;
import org.wisigoth.chat.swing.WisigothController;
import org.wisigoth.chat.swing.WisigothSystemTray;

public class SessionMockUp implements Session {

	private String login;
	private double idSession;
	private Presence presence;
	private WisigothController wisigothController;
	private ConnectionState connectionState;

	private EventListenerList listeners = new EventListenerList();
	private Wisigoth wisigoth;

	public SessionMockUp(Wisigoth wisigoth) {
		this.connectionState = ConnectionState.OFFLINE;
		this.wisigoth = wisigoth;

	}

	/**
	 * Constructor that generate a Session with a hardcoded collection of
	 * contact
	 */
	public Collection<Contact> getContacts() {
		ContactMockUp wiscarol = new ContactMockUp(new WisigothUserMockup(
				"wiscarol"), DefaultPresence.AWAY, AvatarMockUp.POMPOM);
		ContactMockUp java = new ContactMockUp(new WisigothUserMockup("java"),
				DefaultPresence.DND, AvatarMockUp.JESTER);
		ContactMockUp tof = new ContactMockUp(new WisigothUserMockup("tof"),
				DefaultPresence.AVAILABLE, AvatarMockUp.GRENADE);
		ContactMockUp therese = new ContactMockUp(new WisigothUserMockup(
				"therese"), DefaultPresence.AVAILABLE, AvatarMockUp.GIRL);
		ContactMockUp torjon = new ContactMockUp(new WisigothUserMockup(
				"torjon"), DefaultPresence.OFFLINE, AvatarMockUp.FISHED);
		Collection<Contact> friendList = new ArrayList<Contact>();
		friendList.add(wiscarol);
		friendList.add(java);
		friendList.add(tof);
		friendList.add(therese);
		friendList.add(torjon);
		return friendList;
	}

	public ChatSession openChat(Contact contact) {
		ChatSession chatSession = new ChatSessionMockUp(contact);
		return chatSession;

	}

	public Conference openConference(Collection<Contact> contactList) {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not implemented yet");
	}

	public void setPresence(Presence presence) {
		this.presence = presence;
		this.firePresenceChanged();
	}

	private void firePresenceChanged() {
		PresenceEvent event = new PresenceEvent(this);
		for (PresenceListener listener : this.listeners
				.getListeners(PresenceListener.class)) {
			listener.presenceChanged(event);
		}

	}

	@Override
	public Presence getPresence() {
		return this.presence;
	}

	@Override
	public void addPresenceListener(PresenceListener e) {
		this.listeners.add(PresenceListener.class, e);

	}

	@Override
	public void removePresenceListener(PresenceListener e) {
		this.listeners.remove(PresenceListener.class, e);

	}

	@Override
	public void addConnectionStateListener(ConnectionStateListener listener) {
		this.listeners.add(ConnectionStateListener.class, listener);
	}

	@Override
	public ConnectionState getConnectionState() {
		return this.connectionState;
	}

	@Override
	public void removeConnectionStateListener(ConnectionStateListener listener) {
		this.listeners.remove(ConnectionStateListener.class, listener);

	}

	private void fireConnectionStateChanged() {
		ConnectionEvent event = new ConnectionEvent(this);
		for (ConnectionStateListener listener : this.listeners
				.getListeners(ConnectionStateListener.class)) {
			listener.connectionStateChanged(event);
		}
	}

	@Override
	public void reconnect() throws AuthenticationException, ConnectionException {
		if (this.connectionState == ConnectionState.OFFLINE) {
			this.connectionState = ConnectionState.ONLINE;
			this.setPresence(DefaultPresence.AVAILABLE);
			this.fireConnectionStateChanged();
		}

	}

	@Override
	public void disconnect() throws ConnectionException {
		if (this.connectionState == ConnectionState.ONLINE) {
			this.connectionState = ConnectionState.OFFLINE;
			this.fireConnectionStateChanged();

		}
	}

	@Override
	public void authenticate(String login, char[] password)
			throws AuthenticationException {

		String validPassword = ValidUsers.getpassword(login);
		if (validPassword == null) {
			throw new AuthenticationException("Invalid login");
		} else if (!Arrays.equals(validPassword.toCharArray(), password)) {
			throw new AuthenticationException("Invalid Password");
		}
		this.connectionState = ConnectionState.ONLINE;
		WisigothSystemTray.getInstance(this).setMenuOnline();
		this.fireConnectionStateChanged();
	}

	@Override
	public Wisigoth getWisigoth() {
		return this.wisigoth;
	}



	@Override
	public void quitWisigoth() {
		if (this.connectionState == ConnectionState.ONLINE) {

			String presence = this.getPresence().toString();
			this.getWisigoth().getUserPreferences().storePresenceParameters(
					presence);
		}
		System.exit(0);

	}

	@Override
	public void sendMessage(Message message) throws ConnectionException,
			MessageException {
		System.out.println("Message received:" + message);
	}

	@Override
	public WisigothUser whoAmI() throws ConnectionException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setConnectionState(ConnectionState connState) {
		this.connectionState = connState;
		
	}

	@Override
	public FriendList getFriendList() throws FriendListException {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not implemented yet");
	}

	@Override
	public void setFriendList() {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not implemented yet");
	}

	@Override
	public void closeChat(ChatSession chatSession) {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not implemented yet");
	}

	@Override
	public void setUnconsumedMessageConsumer(MessageConsumer consumer) {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not implemented yet");
	}

}
